//---------------------------------------------------------------------------
// Internet Explorer compatibility
//---------------------------------------------------------------------------
if (document.ELEMENT_NODE == null)
{
   document.ELEMENT_NODE = 1;
   document.TEXT_NODE = 3;
}

//-----------------------------------------------------------------------------
//           Popup menu
//-----------------------------------------------------------------------------
function popupMenu(obj, divId, menu, e, offX, offY)
{
	if (!e) e = window.event;
   	e.cancelBubble = true;
   	if (e.stopPropagation) e.stopPropagation();
   
   	// Change DD - FireFox
   	if (navigator.userAgent.indexOf('MSIE') == -1) offX+=170;
    
   	var divObj = document.getElementById(divId);
   
   	//This is to adjust the y offset of the filter menu in IE with datatable headers fixed
   	offY = -divObj.scrollTop;
		 
   	// Change DD - FireFox
 	if (navigator.userAgent.indexOf('MSIE') == -1) offY+=180;

	var divPos = getElementPosition(divObj)
		 
	offX -= divPos.left;
    offY -= divPos.top;
    offY += obj.offsetHeight;

    var absPos = getElementPosition(obj);
		 
		 
    absPos.left += offX;
    absPos.top += offY;
    CMenuPopUpXY(menu,absPos.left, absPos.top)
    return;
}

//-----------------------------------------------------------------------------
//           Get absolute positron of object passed in
//-----------------------------------------------------------------------------
function getElementPosition(obj)
{
   var offsetTrail = obj;
   var offsetLeft = 0;
   var offsetTop = 0;
   while (offsetTrail)
   {
      offsetLeft += offsetTrail.offsetLeft;
      offsetTop += offsetTrail.offsetTop;
      offsetTrail = offsetTrail.offsetParent;
   }
   if (navigator.userAgent.indexOf('Mac') != -1 &&
       typeof document.body.leftMargin != 'undefined')
   {
      offsetLeft += document.body.leftMargin;
      offsetTop += document.body.topMargin;
   }
   return {left:offsetLeft,top:offsetTop};
}

// the following function set class on mouseover

function changeClass(theObj, newClass)
{
   if (theObj.svClass == null)
   {
      theObj.svClass = theObj.className;
      theObj.className = newClass;
   }
}
// this global variable is set and cleared by the dynamic column resizing scripts
var allowMove_ = false;
// the following function reset class on mouseout on header
function restoreClassForHeader(theObj)
{
    if (theObj.svClass != null && allowMove_ == false)
    {
       theObj.className = theObj.svClass;
       theObj.svClass = null;
    }
}
// the following function reset class on mouseout
function restoreClass(theObj)
{
    if (theObj.svClass != null)
    {
       theObj.className = theObj.svClass;
       theObj.svClass = null;
    }
}

//--------------------------------------------------------------------------
// Function to reset odd even style after a sort or filter
//--------------------------------------------------------------------------

function setRowClass(tBody)
{
   var oddDisplay = "datatable-row-odd";
   var evenDisplay = "datatable-row-even";
   var dispRow = 0;
   for (r = 0; r < tBody.rows.length; r++)
   {
      row = tBody.rows[r];
      if (row.style.display == "")
      {
         if (dispRow++ % 2 == 0)
           row.className = evenDisplay;
         else
            row.className = oddDisplay;
      }
   }
}

//---------------------------------------------------------------------------
// Function to get text value for a sort (after first level).or filter
//---------------------------------------------------------------------------
function getTextValue(el)
{
   var i;
   var s;
   // Find and concatenate the values of all text nodes contained within the
   // element.
   s = "";
   for (i = 0; i < el.childNodes.length; i++)
   {
      if (el.childNodes[i].nodeType == document.TEXT_NODE)
      {
         if (el.childNodes[i].nodeValue == datatableBlankCompareValue)
           s += " ";
         else
           s += el.childNodes[i].nodeValue
      }
      else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
                el.childNodes[i].tagName == "BR")
         s += " ";
      else
         // Use recursion to get text within sub-elements.
         s += getTextValue(el.childNodes[i]);
    }
    return s;
}


//---------------------------------------------------------------------------
// Function to compare values during a sort or filter
//---------------------------------------------------------------------------
function compareValues(v1, v2)
{
   // Compare the two values.
   if (v1 == v2)
       return 0;
   if (v1 > v2)
       return 1;
   return -1;
}
//---------------------------------------------------------------------------
// Function to compare values during a sort or filter
//---------------------------------------------------------------------------
function compareStrings(str1, str2)
{
   // Compare the two values.
   return str1.localeCompare(str2)
}

//---------------------------------------------------------------------------
// Functions to set the checkboxes to selected/unselected
//---------------------------------------------------------------------------


function resetSelectedAll(tBody)
{
   var tableId = tBody.id;
   
   
   var col = tBody.selectColumn
   if (isNaN(tBody.selectColumn))
      tBody.selectColumn = -1;
   if ( col < 0) return;
   
   // get field in which selected cells is stored
   var selectedCellsFld = document.getElementById(tableId + datatableSelectedCells);

   // check to see if entire column is selected
   if (selectedCellsFld.value == datatableSelectedAll) {
      doSetSelectedAll(tBody, col, true);
   }
   else
   {
      if(selectedCellsFld.value == "")
      {
         var selCol = document.getElementById('datalist_header_select');

         if(selCol != null)
         {

            if(selCol.checked)
            {
               doSetSelectedAll(tBody, col, selCol.checked);
               return;
            }
         }

      }

      doSetSelectedAll(tBody, col, false);
      var cellsToSelect = selectedCellsFld.value.split(datatableOnLoadDivider);
      for (var i = 0; i <  cellsToSelect.length; i++)
      {
         var cellToSelect = document.getElementById(cellsToSelect[i]);
         if (cellToSelect != null)
         {
            cellToSelect.checked = true;
         }
      }
   }
}

function setSelectedAll(selObj, tableId, col)
{
   // Get the table (tBody) section to sort.
   var tBody = document.getElementById(tableId);
   var selFlag = selObj.checked;
   // get field in which selected columns is stored

   var selectedCellsFld = document.getElementById(tableId + datatableSelectedCells);
   doSetSelectedAll(tBody, col, selFlag)
   if (selFlag == true)
      // set to all selected
      selectedCellsFld.value = datatableSelectedAll;
   else
      //clear
      selectedCellsFld.value = "";
}

function doSetSelectedAll(tBody, col, selFlag)
{
   for (var r = 0; r < tBody.rows.length; r++)
   {
      var row = tBody.rows[r];
      row.cells[col].childNodes[0].checked = selFlag;
   }

}

function setSelected(selObj, tableId, col)
{
   // Get the table (tBody) section
   var tBody = document.getElementById(tableId);
   // get field in which selected columns are stored
   var selectedCellsFld = document.getElementById(tableId + datatableSelectedCells);
   doSetSelectedField(tBody, col, selectedCellsFld);
}

function doSetSelectedField(tBody, col, selectedCellsFld)
{
   var tableId = tBody.id;
   var oldselectedCells = "";
   selectedCellsFld.value = oldselectedCells;
   for (var r = 0; r < tBody.rows.length; r++)
   {
      var row = tBody.rows[r];
      if (row.cells[col].childNodes[0].checked == true)
      {
         selectedCellsFld.value = oldselectedCells +
                                 row.cells[col].childNodes[0].id;
         oldselectedCells =  selectedCellsFld.value + datatableOnLoadDivider;
      }
   }

}
/** trim methods
 * /  Delimiter for literal regular expressions in JavaScript.
 * ^  If a pattern begins with a '^', then the pattern will only be matched
 *    if it occurs at the beginning of the text. So, for example,
 *    the regular expression /^bit/ would be matched in 'bitter', but not in 'gambit'.
 * $  If a pattern ends with a '$', then the pattern will only be matched
 *    if it occurs at the end of the text. So, for example,
 *    the regular expression /bit$/ would be matched in 'gambit', but not in 'bitter'.
 * \s Matches a "whitespace" character. Depending on the regular
 *    expression engine, this can include carriage returns, line feeds,
 *    horizontal tabs and vertical tabs, as well as spaces. Note that this is
 *    case-sensitive: '\S' matches non-whitespace characters.
 * +  Means that the preceding character is matched if it occurs one or more times.
 *    So, for example, the regular expression /pe+p/ would match both 'pep' and 'peep',
 *    but not 'pp'.
 *
 * Using the above syntax we can create the two regular expressions that
 * we're going to need:
 * /^\s+/   Matches one or more whitespace characters at the beginning of the text.
 * /\s+$/   Matches one or more whitespace characters at the end of the text.
 *
 *  Note
 *   There seems to be a sever performance issue with these method if used on very
 *  large table. Consequently they are not currently used.
 */

// Left trim method
function leftTrim(text)
{
   //Match spaces at beginning of text and replace with a null string
   return text.replace(/^\s+/,'');
}


//Right trim method
function rightTrim(text) {
   //Match spaces at end of text and replace with a null string
   return text.replace(/\s+$/,'');
}


// Trim method
function trim(text)
{
   //Match spaces at beginning and end of text and replace
   //with null strings
   return text.replace(/^\s+/,'').replace(/\s+$/,'');
}

//For moz/nn set the scroll property conditionally
function setScrollProperty(tableId)
{
   if ( navigator.userAgent.toLowerCase().indexOf( 'gecko' ) != -1 )
   {
      var tableBody = document.getElementById(tableId);

     if(tableBody==null)
        return;

      var height = self.innerHeight;

     height = Math.round((height * 0.7));

      if(tableBody.offsetHeight >= height)
      {
         tableBody.style.maxHeight = height + 'px';
         tableBody.style.overflow = "-moz-scrollbars-vertical";

         //get div element enclosing the datatable
         var orgTableId = tableId.substring(0,tableId.indexOf('_body_'));

         var divElem = document.getElementById(orgTableId + '_div');

         if(divElem.offsetHeight > height)
            divElem.style.maxHeight = (height+47) + 'px';
      }
      else
      {
         tableBody.style.overflow = "-moz-scrollbars-none";
      }
   }
}


